package com.bianguo.android.camerademo.util;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

/**
 * Created by Administrator on 2018/3/19.
 */

public class CameraUtils {

    public static final String CAMERA_IMAGE = "/sdcard/test.jpg";

    public static final String CAMERA_VIDEO = "/sdcard/test.mp4";

    public static void pickImageFromSystem(Activity activity)
    {
        /**
         * 参数一:打开系统相册的ACTION 参数二:返回数据的方式(从系统相册的数据库获取)
         */
        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(intent,
                RequestCode.FLAG_REQUEST_SYSTEM_IMAGE);
    }

    public static String getImagePathFromSystem(Activity activity, Intent data)
    {
        // TODO 处理从相册返回的图片数据
        Uri uri = data.getData();// 使用getData方法获取要调用的接口
        // 第二个参数表示要查询的数据的字段名
        Cursor c = activity.getContentResolver().query(uri,
                new String[]{MediaStore.Images.Media.DATA}, null, null, null);
        if (c != null)
        {
            c.moveToFirst();
            // 通过游标来获取名为MediaStore.Images.Media.DATA字段的值
            String path = c.getString(c
                    .getColumnIndex(MediaStore.Images.Media.DATA));
            return path;
        }
        return null;
    }

    public static void openCameraForImage(Activity activity)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 仅当设置了MediaStore.EXTRA_OUTPUT参数时,系统将不再返回缩略图,而是会被完整保存以下路径
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File(CAMERA_IMAGE)));
        activity.startActivityForResult(intent,
                CameraUtils.RequestCode.FLAG_REQUEST_CAMERA_IMAGE);
    }

    public static void openCameraForVideo(Activity activity)
    {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        // 仅当设置了MediaStore.EXTRA_OUTPUT参数时,系统将不再返回缩略图,而是会被完整保存以下路径
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File(CAMERA_VIDEO)));
        activity.startActivityForResult(intent,
                RequestCode.FLAG_REQUEST_CAMERA_VIDEO);
    }

    public interface RequestCode
    {
        int FLAG_REQUEST_SYSTEM_IMAGE = 0;

        int FLAG_REQUEST_CAMERA_IMAGE = 1;

        int FLAG_REQUEST_CAMERA_VIDEO = 2;
    }

    public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation,
                                     int viewWidth, int viewHeight) {
        // Need mirror for front camera.
        matrix.setScale(mirror ? -1 : 1, 1);
        // This is the value for android.hardware.Camera.setDisplayOrientation.
        matrix.postRotate(displayOrientation);
        // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
        // UI coordinates range from (0, 0) to (width, height).
        matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
        matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    }
}
